var url="";
var model;
var request;
var Shi;
var result=false;
var result1=false;
var result2=false;
var result3=false;
function LoadData(){
   result=false;
   result1=false;
   result2=false;
   result3=false;

   LoadPieData();
   LoadTotalData();
   LoadSeriesData();
}

function LoadPieData(){
    request.post('http://'+url+'/app/EnergyRealtime!ajaxEnergyConsumptionRealtime?projectId='+GetQueryString('projectId')).success(function(data){
        // if(data!=null&&data.length==0){
        //    $("#body").html("<div style='margin:100px auto;width:200px;height:100px;position: relative;text-align:center;'><img  style='position: relative;' src='image/nodata.png' width='62' height='66'/><br/><span style='font-size:14px;color:rgb(141,141,141);'>没有相关内容!</span></div>");
        //  }else
        //  {
        //  }
        console.log(data);
        var option1 = {
                        backgroundColor: 'white',
                        tooltip: {
                            formatter: "{a} <br/>{c} {b}"
                        },
                        series: [
                            {
                                name: 'COP',
                                type: 'gauge',
                                center: ['50%', '48%'],    // 默认全局居中
                                radius: '80%',
                                startAngle: 200,
                                endAngle: -20,
                                z: 3,
                                min: 0,
                                max: 10,
                                splitNumber: 10,
                                axisLine: {            // 坐标轴线
                                    lineStyle: {       // 属性lineStyle控制线条样式
                                        width: 10,
                                        color: [[data.poor, '#FF4C4C'], [data.medium, '#FFE84C'], [data.good, '#6CCE4F'], [data.excellent, '#5DC8FF']]

                                    }
                                },
                                axisTick: {            // 坐标轴小标记
                                    length: 15,        // 属性length控制线长
                                    lineStyle: {       // 属性lineStyle控制线条样式
                                        color: 'auto'
                                    },
                                    splitNumber: 10
                                },
                                splitLine: {           // 分隔线
                                    length: 20,         // 属性length控制线长
                                    lineStyle: {       // 属性lineStyle（详见lineStyle）控制线条样式
                                        color: 'auto'
                                    }
                                },
                                title: {
                                    offsetCenter: [-5, '60%'],
                                    textStyle: {       // 其余属性默认使用全局文本样式，详见TEXTSTYLE
                                        fontWeight: 'bolder',
                                        fontSize: 18
                                    }
                                },
                                pointer: {
                                    width: 3
                                },
                                detail: {
                                    textStyle: {       // 其余属性默认使用全局文本样式，详见TEXTSTYLE
                                        fontWeight: 'bolder',
                                        fontSize: 20
                                    },
                                    offsetCenter: ['38%', 39],
                                    formatter: '\n{value}\nCOP             '
                                },
                                data: [{value: data.cop, name: " 额定值 " +data.settCopValue+ "\r\n实际值      "}]
                            },
                            {
                                name: '总功率',
                                type: 'gauge',
                                center: ['20%', '40%'],    // 默认全局居中
                                radius: '60%',
                                min: 0,
                                max: data.maxTotalPowerStr,
                                startAngle: 270,
                                endAngle: 45,
                                splitNumber: 6,
                                axisLine: {            // 坐标轴线
                                    lineStyle: {       // 属性lineStyle控制线条样式
                                        color: [[0.2, '#ff4500'], [0.8, '#48b'], [1, '#228b22']],
                                        width: 8
                                    }
                                },
                                axisTick: {            // 坐标轴小标记
                                    length: 10,        // 属性length控制线长
                                    lineStyle: {       // 属性lineStyle控制线条样式
                                        color: 'auto'
                                    },
                                    splitNumber: 10
                                },
                                splitLine: {           // 分隔线
                                    length: 10,         // 属性length控制线长
                                    lineStyle: {       // 属性lineStyle（详见lineStyle）控制线条样式
                                        color: 'auto'
                                    }
                                },
                                pointer: {
                                    width: 3
                                },
                                title: {
                                    offsetCenter: [0, '170%'],      // x, y，单位px
                                    textStyle: {       // 其余属性默认使用全局文本样式，详见TEXTSTYLE
                                        fontWeight: 'bolder',
                                        fontSize: 12
                                    }
                                },
                                detail: {
                                    offsetCenter: [0, '130%'],     
                                    textStyle: {       // 其余属性默认使用全局文本样式，详见TEXTSTYLE
                                        fontWeight: 'bolder',
                                        fontSize: 16
                                    },
                                    width: 0,
                                    height: -10
                                },
                                data: [{
                                    value: data.current_total_power,
                                    name: 'x10 kw\r\n总功率'
                                }]
                            },
                            {
                                name: '总冷量',
                                type: 'gauge',
                                center: ['80%', '40%'],    // 默认全局居中
                                radius: '60%',
                                min: 0,
                                max: data.maxTotalCoolingCapacityStr,
                                startAngle: 135,
                                endAngle: -90,
                                splitNumber: 6,
                                axisLine: {            // 坐标轴线
                                    lineStyle: {       // 属性lineStyle控制线条样式
                                        color: [[0.2, '#ff4500'], [0.8, '#48b'], [1, '#228b22']],
                                        width: 8
                                    }
                                },
                                axisTick: {            // 坐标轴小标记
                                    splitNumber: 10,
                                    length: 10,        // 属性length控制线长
                                    lineStyle: {       // 属性lineStyle控制线条样式
                                        color: 'auto'
                                    }
                                },
                                splitLine: {           // 分隔线
                                    length: 15,         // 属性length控制线长
                                    lineStyle: {       // 属性lineStyle（详见lineStyle）控制线条样式
                                        color: 'auto'
                                    }
                                },
                                pointer: {
                                    width: 3
                                },
                                title: {
                                    offsetCenter: [0, '170%'],      // x, y，单位px
                                    textStyle: {       // 其余属性默认使用全局文本样式，详见TEXTSTYLE
                                        fontWeight: 'bolder',
                                        fontSize: 12
                                    }
                                },
                                detail: {
                                     offsetCenter: [0, '130%'],  
                                    textStyle: {       // 其余属性默认使用全局文本样式，详见TEXTSTYLE
                                        fontWeight: 'bolder',
                                        fontSize: 16
                                    },
                                    width: 0,
                                    height: -10
                                },
                                data: [{
                                    value: data.current_cooling_capacity,
                                    name: 'x100 kw\r\n总冷量'
                                }]
                            }
                        ]
                    };
          myChart.setOption(option1); 
          result1 = true;
          if(result1&&result2&&result3)  result=true;
         }).error(function(data,status,headers,config){
      if((status>=200&&status<300)||status===304||status===1223||status===0){
      $("body").html("网络访问出错！");
      }
      });
}


function LoadTotalData(){
    request.post('http://'+url+'/app/EnergyRealtime!ajaxMachineRoomInfo?projectId='+GetQueryString('projectId')).success(function(data){
          console.log(data);
          model.detail=data;
          result2= true;
          if(result1&&result2&&result3)  result=true;
         }).error(function(data,status,headers,config){
      if((status>=200&&status<300)||status===304||status===1223||status===0){
      $("body").html("网络访问出错！");
      }
      });
}

function LoadSeriesData(){
      request.post('http://'+url+'/app/EnergyRealtime!ajaxEnergyChart?timeRange='+model.timeRange+'&chartType='+model.chartType+'&projectId='+GetQueryString('projectId')).success(function(data){
          console.log(data);

option = {

    backgroundColor:'white',
    grid: {
                            x: 43,
                            y: 23,
                            x2:33,
                            y2:33,
                            width:'85%'
                        },
    calculable : false,
    xAxis : [
        {
            type : 'category',
            data : data.xAxisData
        }
    ],
    yAxis : [
        {
            type : 'value'
        }
    ],
    series : [
        {
            name:'冷水机',
            type:model.chartType,
            stack:1,
            data:data.chiller_energy_cost
        },
       {
            name:'冷却机',
            type:model.chartType,
            stack:1,
            data:data.cooling_pump_energy_cost
        },
        {
            name:'冷冻机',
            type:model.chartType,
            stack:1,
            data:data.chilled_water_pump_energy_cost
        },
        {
            name:'冷却塔',
            type:model.chartType,
            stack:1,
            data:data.cooling_tower_energy_cost
        }
    ]
}; 
    //LoadBar();
  if(model.chartType=='line')  LoadLine();
  else if(model.chartType=='bar') LoadBar();
            result3 = true;
          if(result1&&result2&&result3)  result=true;
    //myChart2.setOption(option); 
         }).error(function(data,status,headers,config){
      if((status>=200&&status<300)||status===304||status===1223||status===0){
      $("body").html("网络访问出错！");
      }
      });
}

(function(){
	var app=angular.module('Shi',[ ]);
	app.controller('ShiController',['$http','$window',function($http,$window){
     Shi=this;
	request=$http;
    model=Shi;
    bindEvent();
    Shi.showflag=false;
    Shi.showflag2=false;
    Shi.showfont='全天';
    Shi.showfont2='柱状图';
    Shi.timeRange='all_day';
    Shi.chartType='bar';
    url='120.26.110.125/jncloud';
    Shi.show={'display':'none'};
    Shi.showstyle={'display':'none'};
    Loadgauge();

    LoadData();

    LoadPieData();
    LoadTotalData();
    LoadSeriesData();


    Shi.Showpean=function(){
      if(!Shi.showflag)
      {
        Shi.showflag=true;
        Shi.show={'display':''};
      }
      else
      {
        Shi.showflag=false;
        Shi.show={'display':'none'};
      }
    }

    Shi.Showchoosestyle=function(){
      if(!Shi.showflag2)
      {
        Shi.showflag2=true;
        Shi.showstyle={'display':''};
      }
      else
      {
        Shi.showflag2=false;
        Shi.showstyle={'display':'none'};
      }
    }

    Shi.showQuan=function(){
       Shi.show={'display':'none'};
       Shi.showflag=false;
       Shi.showfont="全天";
       Shi.timeRange='all_day';
       LoadSeriesData();
    }

    Shi.showSi=function(){
        Shi.show={'display':'none'}; 
        Shi.showflag=false;

        Shi.showfont="四小时内";
        Shi.timeRange='four_hour';
        LoadSeriesData();
    }

    Shi.showYi=function(){
         Shi.show={'display':'none'};
         Shi.showflag=false;
         Shi.showfont="一小时内";
         Shi.timeRange='two_hour';
         LoadSeriesData();
    }

    Shi.showBan=function(){
         Shi.show={'display':'none'};
         Shi.showflag=false;
         Shi.showfont="半小时内";
         Shi.timeRange='one_hour';
         LoadSeriesData();
    }

    Shi.showZhu=function(){
         Shi.showstyle={'display':'none'};
         Shi.showflag2=false;
         Shi.showfont2="柱状图";
         Shi.chartType='bar';
         LoadSeriesData();
    }
    Shi.showQu=function(){
         Shi.showstyle={'display':'none'};
         Shi.showflag2=false;
         Shi.showfont2="曲线图";
         Shi.chartType='line';
         LoadSeriesData();
    }

    // Shi.quxiao=function(){
    //     alert();
    //     if(Shi.showflag2)
    //     {
    //      Shi.showstyle={'display':'none'};
    //      Shi.showflag2=false;  
    //     }
    //     if(Shi.showflag)
    //     {
    //     Shi.show={'display':'none'};
    //      Shi.showflag=false;  
    //     }
    // }
	}]);
})();
